package com.example.black.myapplication.objectanimator;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;

import com.example.black.myapplication.R;

public class ObjectAnimatorActivity extends AppCompatActivity {

    private Button btn1;
    private int position = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_object_animator);
        initView();
    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(this::startAnimation);
    }

    public void startAnimation(View v){
        btn1.setTranslationX(position);
//        btn1.setAlpha((float) Math.random());
//        position = position + 20;
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(v,"translationX",0f,300f);
//        objectAnimator.setDuration(500);
//        objectAnimator.start();
//        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(v,"translationY",0f,300f);
//        objectAnimator1.setDuration(500);
//        objectAnimator1.start();
//        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(v,"rotationX",0f,360f);
//        objectAnimator2.setDuration(500);
//        objectAnimator2.start();

//        ObjectAnimator animator =  ObjectAnimator.ofFloat(v,"haha",0f,100f);//没有这个属性的时候，就是valueanimator
//        animator.setDuration(500);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                animation.getAnimatedFraction();
//                float value = (float) animation.getAnimatedValue();
//                btn1.setScaleX(0.5f+value/200);
//                btn1.setScaleY(0.5f+value/200);
//            }
//        });
//        animator.start();

//        ValueAnimator animator = ValueAnimator.ofFloat(0f,100f);
//        animator.setDuration(200);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                animation.getAnimatedFraction();
//                float value = (float) animation.getAnimatedValue();
//                btn1.setScaleX(0.5f+value/200);
//                btn1.setScaleY(0.5f+value/200);
//            }
//        });
//        animator.start();

//        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha",1f,0.7f,1f);
//        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX",1f,0.7f,1f);
//        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY",1f,0.7f,1f);
//        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(btn1,holder1,holder2,holder3);
//        animator.setDuration(1000);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float animatedValue = (float) animation.getAnimatedValue();
//				float animatedFraction = animation.getAnimatedFraction();
//				long playTime = animation.getCurrentPlayTime();
//				System.out.println("animatedValue:"+animatedValue+",  playTime:"+playTime);
//            }
//        });
//        animator.start();

//        ObjectAnimator animator1 = ObjectAnimator.ofFloat(btn1,"alpha",1f,0.7f,1f);
//        ObjectAnimator animator2 = ObjectAnimator.ofFloat(btn1,"scaleX",1f,0.7f,1f);
//        ObjectAnimator animator3 = ObjectAnimator.ofFloat(btn1,"scaleY",1f,0.7f,1f);
//
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.setDuration(500);
//       // animatorSet.play(animator1);
//       // animatorSet.playTogether(animator1,animator2,animator3);
//        animatorSet.playSequentially(animator1,animator2,animator3);
//        animatorSet.start();

        //实现抛物线效果
        ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setDuration(4000);
        valueAnimator.setObjectValues(new PointF(0,0));
        valueAnimator.setEvaluator(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                //x = v * t;
                PointF pointF = new PointF();
                pointF.x = 100f * (fraction * 4);
         //       pointF.y = 0.5f * 9.8f * (fraction *4)*(fraction*4);//抛物线
                pointF.y = 0.5f * 50f * (fraction *4)*(fraction*4);//查看更明显效果
                return pointF;
            }
        });
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                btn1.setX(pointF.x);
                btn1.setY(pointF.y);

            }
        });
        valueAnimator.start();//如何停止？？

       // ObjectAnimator oa = ObjectAnimator.ofFloat(btn1,"translationY",0f,500f);
       // oa.setDuration(500);
        //oa.setInterpolator(new BounceInterpolator());
       // oa.setInterpolator(new AccelerateInterpolator(5));
		//oa.setInterpolator(new AccelerateDecelerateInterpolator());
	//	oa.setInterpolator(new AnticipateInterpolator(8));
		//oa.setInterpolator(new OvershootInterpolator());
	//	oa.setInterpolator(new CycleInterpolator(4));
       // oa.start();
    }
}
